package cn.gz.demo.ui;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import cn.gz.demo.R;
import cn.gz.demo.databinding.PopupBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.DisplayUtil;


/**
 * Created by Liuyz on 2018/7/5.
 */

public class PopupActivity extends BaseActivity {
    private PopupBinding binding;
    private PopupWindow popupwindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_popup);

        binding.mineActiviteyCenterSevenLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRefreshPopup();
            }
        });
    }

    private void showRefreshPopup() {
        if (popupwindow == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View contentView = layoutInflater.inflate(R.layout.mine_activity_center_seven_popup, null);

            RelativeLayout layout = (RelativeLayout) contentView.findViewById(R.id.mine_activity_center_seven_popup_layout);
            TextView hint = (TextView) contentView.findViewById(R.id.mine_activity_center_seven_popup_hint);

            hint.setText("哇~被你发现啦！悄悄告诉你：连续\n签到6天可获得X+N积分大礼包！");
            popupwindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popupwindow.setAnimationStyle(R.style.picture_center_dialog);
            popupwindow.setFocusable(true);
            popupwindow.setOutsideTouchable(true);
            popupwindow.setBackgroundDrawable(new BitmapDrawable());
        }
        // 获取点击坐标
//        popupwindow.showAtLocation(binding.mineActiviteyCenterSevenIcon, Gravity.TOP, 0, 0);
//        popupwindow.showAsDropDown(binding.mineActiviteyCenterSevenIcon, -2000, -200);

        int[] location = new int[2];
        binding.mineActiviteyCenterSevenIcon.getLocationOnScreen(location);
        int offX = (screenWidth - DisplayUtil.dip2px(context, 30)) / 14 + DisplayUtil.dip2px(context, 15);
        popupwindow.showAtLocation(binding.mineActiviteyCenterSevenIcon, Gravity.TOP, offX, location[1] - DisplayUtil.dip2px(context, 78));  //显示
    }
}
